/**
 * Created by admin on 4/14/16.
 */

(Index = function ($) {
    var init_role_list, init, current_role_url = "", current_object_url = "",
        current_object_list = null, update_content_by_objects,
        update_content_by_object,
        $role_menu_title = $("#role_menu_title"),
        $role_menu = $(".role_menu_list"),
        $object_menu = $(".object_menu_list"),
        $object_menu_title = $("#object_menu_title"),
        $content = $('#content_panel'),
        update_object_change;

    // 件切换操作对象事件
    update_object_change = function(url){
        $.ajax({
            url:url,
            success : function(result){
                $content.html(result)
            }
        })
    }

    update_content_by_objects = function (obj_list) {
        var $row, $col
        $(window).unbind("resize")
        $row = $('<div class="row" style="padding-right:15px"></div>')
        for (index in current_object_list) {
            $col = $(
                '<div class="col-sm-6 col-md-3">' +
                    '<div class="thumbnail">' +
                        '<img data-src="holder.js/300x200" alt="">' +
                        '<h3><a href="' + current_object_list[index].url +'">'+ current_object_list[index].name +'</a></h3>' +
                        '<p>缩略图描述...</p>' +
                    '</div>' +
                '</div>'
            )
            $row.append($col)
            $content.html("")
            $content.append($row)
            $row.find("a").click(function(event){
                var $this = $(this)
                $object_menu_title.html('管理<em style="color:green">' + $this.text() + '</em>对象')
                update_object_change($this.attr("href"))
                return false
            })
        }
    }

    update_content_by_object = function () {

    }

    $("#role").click(function (event) {
        if (event.target.tagName === 'EM') {
            update_content_by_objects(current_object_list)
            $object_menu_title.html('管理...')
            return false
        }
    })

    $("#obj").click(function (event) {
        if (event.target.tagName === 'EM') {
            return false
        }
    })

    init_role_list = function () {
        // 给角色名称更改下拉菜单绑定事件
        $role_menu.find('li a').click(function (event) {
            var $this = $(this), url = $this.attr('href')
            if (current_role_url === url) {
                $role_menu_title.trigger('click')
                return false
            }
            current_role_url = url
            $role_menu_title.html('我是<em style="color:green">' + $this.text() + '</em>')
                .trigger('click')

            $.ajax({
                url: url,
                success: function (result) {
                    // 根据角色名称返回对应的操作对象后进行操作对象下拉菜单的

                    $object_menu.html('')
                    for (index in result) {
                        $object_menu.append('<li><a href="' + result[index].url + '">' + result[index].name + '</a></li>')
                    }
                    current_object_list = result

                    update_content_by_objects(current_object_list)
                    $object_menu.find('li a').click(function (event) {
                        var $this = $(this), url = $this.attr('href')
                        if (current_object_url === url) {
                            $object_menu_title.trigger('click')
                            return false
                        }
                        current_object_url = url
                        $object_menu_title.html('管理<em style="color:green">' + $this.text() + '</em>对象')
                            .trigger('click')
                        update_object_change(url)
                        return false
                    })
                }
            })
            return false
        })
    }

    init = function () {
        init_role_list()
    }
    return {
        init: init
    }
}(jQuery));